// pages/classify/classify.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _num:0,
    nav: [],
    cate:[]
  },
  focus_click:function(e){
    wx.showLoading({
      title:"加载中"
    })
    var that=this;
    wx.request({
      url: app.data.Url +'goods/lists',
      data:{
        // bid:123,
        bid: app.data.address.id,
        tid: e.currentTarget.id
      },
      method:'post',
      success:function(res){
        // that.setData({
        //   care: res.data.data,
        //   _num: e.target.dataset.index
        // })
        // wx.hideLoading()
        // console.log(res.data.data)
        // wx.setStorage({
        //   key: "tid",
        //   data: e.currentTarget.id,
        // })
        console.log(res)
        
      }
    })

  },
  search: function () {
    wx.navigateTo({
      url: '../search/seller',
    })
  },
  click:function(e){
    // return false
    var ptid = e.currentTarget.dataset.ptid 
    var tid = e.currentTarget.dataset.tid 
    app.navTo('../assortment/assortment?ptid=' + ptid + '&tid=' + tid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var that = this;
    // var _num = that.data._num;
    // console.log(app.data.address.id,'app.data.address.id')
    // wx.setStorageSync('classlist', '')
    // 判断是否从一级入口点入
    
    wx.getStorage({
      key: "tid",
      success: function (res) {
        let tid = res.data;
        if (tid) {
          wx.request({
            url: app.data.Url + 'menus/menus',
            data: {
              // bid: 123
              bid: app.data.address.id
            },
            method: 'get',
            success: function (res) {
              console.log(res.data.data,'-----------')
              let menusParent = res.data.data;
              let tempTid = -1;
              let tempCurrentIndex = -1;
              menusParent.forEach(function (item, index) {
                // console.log(index)
                if (item.tid == tid) {
                  tempCurrentIndex = index;
                  tempTid = item.tid;
                }
              });
              wx.request({
                url: app.data.Url + 'commtype/twomenus',
                data: {
                  // bid: 123,
                  bid: app.data.address.id,
                  tid: tempTid
                },
                success: function (res) {
                  that.setData({
                    care: res.data.data
                  })
                  wx.hideLoading()
                }
              })
              that.setData({
                cate: res.data.data,
                _num: tempCurrentIndex
              })
            }
          })
          return false;
        }
      },
      fail: function () { // 无参数
        wx.request({
          url: app.data.Url + 'menus/menus',
          data: {
            bid: app.data.address.id
          },
          method: 'get',
          success: function (res) {
            wx.request({
              url: app.data.Url + 'commtype/twomenus',
              data: {
                bid: app.data.address.id,
                tid: res.data.data[0].tid
              },
              success: function (res) {
                console.log(res.data.data)
                that.setData({
                  care: res.data.data,
                  _num: 0
                })
                wx.hideLoading()
              }
            })
            that.setData({
              cate: res.data.data
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    wx.removeStorage({
      key: 'tid',
      success: function (res) {
        that.setData({
          _num: res.data.data,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // wx.removeStorage({
    //   key: 'tid',
    //   success: function (res) {
    //     that.setData({
    //       _num: res.data.data,
    //     })
    //   }
    // })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})